package des.wangku.operate.standard.task;

import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.events.HelpEvent;
import org.eclipse.swt.events.HelpListener;
import org.eclipse.swt.widgets.Composite;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import des.wangku.operate.standard.dialog.AbstractSearchDialog;
import des.wangku.operate.standard.dialog.DialogShow;
import des.wangku.operate.standard.swt.InterfaceMultiSave;
import des.wangku.operate.standard.swt.InterfaceMultiTreeExtend;
import des.wangku.operate.standard.utls.UtilsSWTTools;

/**
 * 容器任务主父类<br>
 * 使用线程池。具体的池化没有做。运行一次，建立一次线程池，同线程组
 * @author Sunjian
 * @version 1.0
 * @since jdk1.8
 */
public abstract class AbstractTask extends AbstractTask3_Thread implements InterfaceTaskLoad,InterfaceTablesDialog, InterfacePropertiesObject, InterfaceMultiSave, InterfaceJson, 
		InterfaceCompositeMenu, InterfaceMultiTreeExtend, InterfaceSubject {
	/** 日志 */
	static Logger logger = LoggerFactory.getLogger(AbstractTask.class);
	/** 本父类对象 */
	protected AbstractTask abstractTask = this;
	/** 搜索时弹出的窗口 */
	AbstractSearchDialog searchDialog = null;

	@Override
	public void setSearchDialog(AbstractSearchDialog e) {
		searchDialog = e;
	}

	@Override
	public AbstractSearchDialog getSearchDialog() {
		return searchDialog;
	}

	/**
	 * 构造函数
	 * @param parent 父容器
	 * @param style 状态
	 */
	public AbstractTask(Composite parent) {
		super(parent);
	}

	/**
	 * 构造函数
	 * @param parent 父容器
	 * @param style 状态
	 */
	public AbstractTask(Composite parent, int style) {
		super(parent, style);
		Class<? extends AbstractTask> basicClass = this.getClass();
		init(parent, style, basicClass, MVDefault);
	}

	/**
	 * 构造函数
	 * @param parent 父容器
	 * @param style 状态
	 * @param basicClass 子类
	 */
	public AbstractTask(Composite parent, int style, Class<? extends AbstractTask> basicClass) {
		super(parent, style);
		init(parent, style, basicClass, MVDefault);
	}

	/**
	 * 构造函数
	 * @param parent 父容器
	 * @param style 状态
	 * @param basicClass 子类
	 * @param abstractMenuValue 显示鼠标右键的功能
	 */
	public AbstractTask(Composite parent, int style, Class<? extends AbstractTask> basicClass, int abstractMenuValue) {
		super(parent, style);
		init(parent, style, basicClass, abstractMenuValue);
	}

	/**
	 * 初始化
	 * @param parent 父容器
	 * @param style 状态
	 * @param basicClass 子类
	 * @param abstractMenuValue int 显示鼠标右键的功能
	 */
	private final void init(Composite parent, int style, Class<? extends AbstractTask> basicClass, int abstractMenuValue) {
		this.basicClass = basicClass;
		this.abstractMenuValue = abstractMenuValue;
		super.initialization();
		initListener();
	}


	/** 具体的模块的类 */
	protected Class<? extends AbstractTask> basicClass = null;

	public Class<?> getBasicClass() {
		return basicClass;
	}

	public void startup_Load() {
		super.startup_Load();
	}
	/**
	 * 关闭所有资源与项目
	 */
	public void disposeAll() {
		disposeResources();
		disposeProject();
		super.disposeTask();
	}


	/**
	 * 初始化<br>
	 * 添加监听器
	 */
	void initListener() {
		addDisposeListener(new DisposeListener() {
			public void widgetDisposed(DisposeEvent e) {
				logger.debug("DisposeListener 被调用 任务完成，进行回收资源信息与项目信息！");
				disposeAll();
			}
		});
		addHelpListener(new HelpListener() {
			@Override
			public void helpRequested(HelpEvent e) {
				DialogShow.showTaskVersion(abstractTask);
			}
		});
	}	
	
	
	
	
	public void startup_afterRepaintComposite() {
		super.startup_afterRepaintComposite();
	}
	/**
	 * 判断容器是否是项目，如果不是，则返回null
	 * @param parent Composite
	 * @return AbstractTask
	 */
	public static final AbstractTask getTask(Composite parent) {
		AbstractTask t = UtilsSWTTools.getParentObjSuperclass(parent, AbstractTask.class);
		return t;
	}

}
